$("#addCategory").on("submit", function () {
  const formData = $(this).serialize();
  $.ajax({
    type: 'post',
    url: '/categories',
    data: formData,
    success: function (response) {
      location.reload()
    }
  })
  return false;
})

// 获取分类数据
$.ajax({
  type: 'get',
  url: '/categories',
  success: function (response) {
    console.log(response);
    const html = template("categoryTpl", {
      data: response
    });
    $("#categoryBox").html(html);
  }
})

// 分类数据修改功能
$("#categoryBox").on("click", ".edit", function () {
  const id = $(this).attr("data-id");
  $.ajax({
    type: 'get',
    url: '/categories/' + id,
    success: function (response) {
      const html = template("modifyTpl", response);
      $("#formBox").html(html);
    }
  })
})

// 提交修改事件
$("#formBox").on("submit", "#modifyCategory", function () {
  const id = $(this).attr("data-id");
  const formData = $(this).serialize();
  $.ajax({
    type: 'put',
    url: '/categories/' + id,
    data: formData,
    success: function (response) {
      location.reload();
    }
  })
  return false;
})

// 删除数据分类功能
$("#categoryBox").on("click", ".delete", function () {
  if(confirm("您真的要删除这个分类吗")) {
    const id = $(this).attr("data-id");
    console.log(id);
    $.ajax({
      type: 'delete',
      url: '/categories/' + id,
      success: function () {
        location.reload();
      }
    })    
  }
})